﻿using Newtonsoft.Json.Linq;
using SimpleSpider.Framework.Downloader;
using SimpleSpider.Framework.Model;
using SimpleSpider.Project.LvShi.Model;
using SimpleSpider.Project.LvShi.Services.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleSpider.Framework
{
    public class EasyCrawlContentWorker : CrawlContentWorker
    {
        string _ip = string.Empty;
        string _port = string.Empty;

        ContentDataService _contentDataService = null;

        int   _retryCount = 0;

        public EasyCrawlContentWorker()
        {

            _contentDataService = new ContentDataService();
        }


        void GetIp()
        {

            _ip = string.Empty;
            _port = string.Empty;

            WebClient webClient = new WebClient();
            string s = webClient.DownloadString("https://sch.shanchendaili.com/api.html?action=get_ip&key=HU97a770213977759925w62e&time=5&count=1&type=json&only=1");

            JToken dailiToken = JObject.Parse(s);
            _ip = dailiToken["list"][0]["sever"].ToString();
            _port = dailiToken["list"][0]["port"].ToString();
        }

        public override void Handler(UrlModel urlModel)
        {
            IDownloader _downloader = null;

            _downloader = new HttpDownloader();
            _downloader.SetRequest("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+");
            _downloader.SetProxyIP(_ip, _port);

            Thread.Sleep(1000);
            string url = "https://lvmou.so.com/v1/law/lawyersearch?id=" + urlModel.UniqueId + "&sort=-score";
            string shtml = string.Empty;
            try
            {
                shtml  = _downloader.Get(url);
            }
            catch (Exception ex)
            {
                _retryCount++;
                if(_retryCount > 0) GetIp();
            }

            _retryCount = 0;

            JToken jToken = JObject.Parse(shtml);
            if ((int)jToken["code"] == 20001)
            {
                GetIp();
                //string shtml2 = _downloader.Get("https://2024.ip138.com/");
            }
            JToken dataToken = jToken["data"]["results"][0];
            //用c#写一个方法

            string email = dataToken["contact"]["email"].ToString();
            string address = dataToken["contact"]["address"].ToString();
            string tel = dataToken["contact"]["tel"].ToString();
            string number = dataToken["number"].ToString();
            string major = dataToken["major"].ToString();
            string majorTerm = dataToken["major_term"].ToString();
            string gender = dataToken["gender"] != null ? dataToken["gender"].ToString() : "-";
            string lawFirm = dataToken["lawFirm"].ToString();
            string provinceTerm = dataToken["province_term"][0].ToString();
            string lawyerName = dataToken["lawyerName"].ToString();
            string year = dataToken["year"].ToString();

            ContentModel contentModel = new ContentModel();
            contentModel.UrlId = urlModel.Id;
            contentModel.StartUrlId = urlModel.StartUrlId;
            contentModel.Title = lawyerName;
            contentModel.Content = lawyerName;

           int id =  Save(contentModel);

            if (id > 0)
            {
                ContentDataModel contentDataModel = new ContentDataModel();
                contentDataModel.contentId = id;
                contentDataModel.email = email;
                contentDataModel.address = address;
                contentDataModel.tel = tel;
                contentDataModel.number = number;
                contentDataModel.major = major;
                contentDataModel.majorTerm = majorTerm;
                contentDataModel.gender = gender;
                contentDataModel.lawFirm = lawFirm;
                contentDataModel.provinceTerm = provinceTerm;
                contentDataModel.lawyerName = lawyerName;
                contentDataModel.year = year;

                _contentDataService.Insert(contentDataModel);

            }

            urlModel.Status = Reborn.Contracts.State.TaskStatus.RanToCompletion;
        }
    }
}
